package us.kg.kotoriforest.mybatis.util;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.InputStream;

/**
 * SqlSession工具类
 *
 * @author Kotori rotroutine@163.com
 * @since 2025/1/11
 */
public final class SqlSessionUtil {
    private static final Logger logger = LoggerFactory.getLogger(SqlSessionUtil.class);
    private static final SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();

    private SqlSessionUtil() {
    }

    public static SqlSession getSqlSession() {
        SqlSession sqlSession = null;
        try (InputStream resource = Resources.getResourceAsStream("mybatis-config.xml")) {
            SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(resource);
            sqlSession = sqlSessionFactory.openSession(true);
        } catch (IOException e) {
            logger.warn(e.toString());
        }

        return sqlSession;
    }
}
